import Vue from 'vue'
import "@/assets/js/common.js"
import router from './router/index.js'
Vue.directive('test', {
    inserted(el,bindings,VNode) {
        console.log(el,bindings,VNode)
        //el.parentNode.removeChild(el);
    },
    update(el,bindings,VNode){
       // console.log(el,bindings,VNode)
        //el.parentNode.removeChild(el);
    }
})

/* 
权限判断-对路由拦截 
用法v-routePms="{name:'home',path:'/home'}"//路由名称和路由path
*/
  Vue.directive('routePms', {
    inserted(el,bindings,VNode) {
        el.addEventListener("click",function(){
            var permission=window.rootVue.$store.state.permission
            var resultList=[]
            
            travers(permission,bindings.value.name,resultList)
			
            if(resultList.length===0){
				router.push({path:bindings.value.path})
               // router.push("/index/no-permission")
               //  Vue.prototype.$message({
               //      type: 'warning',
               //      message: '抱歉,您没有访问权限!',
               //      showClose: true,
               //  })
                
            }else{
                router.push({path:bindings.value.path})
            }
        },false)
    }
  })


  
  //权限指令:适用菜单||按钮,
  //控制方式：不显示
  Vue.directive('pms', {
    inserted(el,bindings,VNode) {
        var permission=window.rootVue.$store.state.permission
        var resultList=[]
        //travers(permission,bindings.value,resultList)
		var bool=permission.indexOf(bindings.value);
        if(bool===-1){
            el.parentNode.removeChild(el);
        }
    }
  })


//对象遍历查询
function travers(obj,value,resultList){
    if(!obj || !obj instanceof Object){
      return
    }
    if(Object.prototype.toString.call(obj)==="[object Array]"){
        if(obj.length===0){
            return
        }
        obj.forEach((item)=>{
            travers(item,value,resultList)
        })
    }else if(Object.prototype.toString.call(obj)==="[object Object]"){
        if(obj.code===value){
			resultList.push(obj)
            return
        }
        if(obj.children && obj.children instanceof Array){
            obj.children.forEach((item)=>{
                travers(item,value,resultList)
            })
          }else{
              return
          }
    }
}


//el-dislog辅助指令，v-draggable,可拖拽
Vue.directive('draggable', {
    bind(el, binding, vnode, oldVnode) {
        //el-dialog  position：absolute定位和position：relative定位处理方法不一样
        const elDialog = el.querySelector('.el-dialog');
        const elDialogHeader = el.querySelector('.el-dialog__header');
        elDialogHeader.style.cursor = 'move';
        var startX=null
        var startY=null
        var distX=null
        var distY=null
        var defaultLeft=null//elDialog初始left
        var defaultTop=null//elDialog初始top
        var defaultOffsetLeft=null
        var defaultOffsetTop=null
        //边界
        var minLeft=0
        var maxLeft=document.body.clientWidth - elDialog.offsetWidth
        var minTop=0
        var maxTop=document.body.clientHeight - elDialog.offsetHeight
        elDialogHeader.onmousedown = (ev) => {
            startX=ev.clientX
            startY=ev.clientY
            defaultLeft=parseInt(getDomStyle(elDialog,'left')) || 0 
            defaultTop=parseInt(getDomStyle(elDialog,'top')) ||0
            
            defaultOffsetLeft=elDialog.offsetLeft
            defaultOffsetTop=elDialog.offsetTop

            document.onmousemove = function (ev) {
                distX=ev.clientX-startX
                distY=ev.clientY-startY
                elDialog.style.left=defaultLeft+distX+"px"
                elDialog.style.top=defaultTop+distY+"px"

                console.log(defaultLeft+distX,defaultTop+distY,elDialog.style.left,elDialog.style.top)
            };
        }
        document.onmouseup = function (ev) {
            document.onmousemove = null;
        }; 

        //获取dom某样式
        function getDomStyle(dom,property){
            if (window.document.currentStyle) {//ie
                return dom.currentStyle[property]
            } else{//非ie
                return window.getComputedStyle(dom, null)[property];
            }
        }
        
      }
})

//el-table辅助指令，使用方法：在el-table组件设置 v-height="'auto'" ，即可是实现表头固定和表格高度随父元素自适应
Vue.directive('height', {
    bind(el, binding, vnode, oldVnode) {
        //console.log(el,binding,vnode)
		setTimeout(()=>{
			var pHeight=el.parentNode.offsetHeight;
			var ethw=el.querySelector(".el-table__header-wrapper");
			if(!ethw){return}
			var tableHeadHeight=ethw.offsetHeight;
			var tablebody=el.querySelector(".el-table__body-wrapper");
			if(tablebody && pHeight>0){
				tablebody.style.height=pHeight-tableHeadHeight +"px";
				tablebody.style.overflowY="auto";
			}
		},10)
    },
	
})






















